import 'dotenv/config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as fs from 'node:fs';
import { HttpExceptionFilter } from './filter/http-exception.filter';
import { AllExceptionsFilter } from './filter/all-exception.filter';
import { TransformInterceptor } from './intecepter/transform.interceptor';
import { NestExpressApplication } from '@nestjs/platform-express';
import * as compression from 'compression';
import * as path from 'path';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  app.useGlobalFilters(new HttpExceptionFilter());
  // 异常处理
  app.useGlobalFilters(new AllExceptionsFilter());
  // 全局结果处理
  app.useGlobalInterceptors(new TransformInterceptor());

  // 文档
  const config = new DocumentBuilder()
    .setTitle('Nestjs Admin')
    .setDescription('The Nestjs Admin API description')
    .setVersion('1.0')
    .addTag('Nestjs Admin')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  fs.writeFileSync(
    path.join(__dirname, '../public/api.json'),
    JSON.stringify(document),
  );
  SwaggerModule.setup('api', app, document);
  // 跨域
  app.enableCors();
  // 静态资源
  app.useStaticAssets('public');
  // 压缩请求
  app.use(compression());
  await app.listen(3000);
}
bootstrap();
